﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlBaoCaoTyLeTVVHoatDong : System.Web.UI.UserControl
{
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    MethodClassCommon common;
    ClassExecute.ClassExecute dt;
    string usertype = "";
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                Session["username"] = "admin";
                common = new MethodClassCommon();
                MultiView1.ActiveViewIndex = 0;
                displayMonthList();
                usertype = common.GetDataDepend("GET_USERTYPE", "UserName", Session["username"].ToString());
                displayTTPP(usertype);
            }
            else
            {
                Bindreport();
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    void displayTTPP(string usertype)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_SITENAME_ALL", CommandType.StoredProcedure);
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "sitecode";
        if (usertype == "ad")
        {
            DropTTPP.SelectedValue = "ALL";
            DropTTPP.DataBind();
            DropTTPP.Enabled = true;
        }
        else
        {
            DropTTPP.SelectedValue = Session["sitecode"].ToString();
            DropTTPP.DataBind();
            DropTTPP.Enabled = false;
        }
       
    }
    void displayMonthList()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "get_MonthListReport", CommandType.StoredProcedure);
        DropMonthList.DataSource = db;
        DropMonthList.DataTextField = "Names";
        DropMonthList.DataValueField = "Names";
        DropMonthList.DataBind();
    }
    void Bindreport()
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "K_Report_Tinh_TyLe_TVVActive", CommandType.StoredProcedure, new SqlParameter("@MMYYYY", DropMonthList.SelectedValue.ToString()), new SqlParameter("@tableName1", Session["username"].ToString() + "_TyLeHoatDong1"), new SqlParameter("@tableName2", Session["username"].ToString() + "_TyLeHoatDong2"), new SqlParameter("@tableName3", Session["username"].ToString() + "_TyLeHoatDong3"), new SqlParameter("@sitecode", DropTTPP.SelectedValue.ToString()));
            if (db.Rows.Count > 0)
            {
                float sumTVVactive = 0;
                float sumTVVmoreThan = 0;
                float total = 0;
                foreach (DataRow dr in db.Rows)
                {
                    sumTVVactive = sumTVVactive + float.Parse(dr["SLTVVActive"].ToString());
                    sumTVVmoreThan = sumTVVmoreThan + float.Parse(dr["SLTVVMoreThan500"].ToString());
                }
              
                total =(float)Math.Round(sumTVVmoreThan / sumTVVactive * 100,2);
                ReportBaoCaoTyLeTVVHoatDong rp = new ReportBaoCaoTyLeTVVHoatDong();
                rp.Parameters["month"].Value = DropMonthList.SelectedValue.ToString();
                rp.Parameters["tyle"].Value = total.ToString();
                rp.DataSource = db;
                ReportViewer1.Report = rp;
                ReportViewer1.DataBind();
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                MultiView1.ActiveViewIndex = 0;
               // uscMsgBox1.AddMessage("không có dữ liệu", uscMsgBox.enmMessageType.Error);
            }
        }
        catch 
        {
            //uscMsgBox1.AddMessage(e.Message, uscMsgBox.enmMessageType.Error);
        }
        
       
    }
    protected void ButtonXem_Click(object sender, EventArgs e)
    {
        try
        {
            Bindreport();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
}
